package cn.jimmie.learn.viewArt.captcha.toolbox;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.graphics.*;
import android.support.v4.view.animation.FastOutLinearInInterpolator;
import cn.jimmie.learn.viewArt.captcha.internal.AnimationFuncAdapter;
import cn.jimmie.learn.viewArt.captcha.internal.SeekCaptchaView;


/**
 * function : 模仿 bilibili 滑块验证动画效果
 *
 * <p> Create Time: 2018年12月09日 17:18 </p>
 * <p> @author Jimmie.Qian  </p>
 */
public class FlashAnimationFunc extends AnimationFuncAdapter {
    private Paint mSuccessPaint;
    private Path mSuccessPath;
    private boolean isShowSuccessAnim;
    private int mSuccessAnimOffset;

    public FlashAnimationFunc(SeekCaptchaView view) {
        super(view);
    }

    @Override
    public void init() {
        super.init();

        int width = mView.getWidth();
        int height = mView.getHeight();

        mSuccessPaint = new Paint();

        // 透明和半透明之间渐变
        mSuccessPaint.setShader(new LinearGradient(0, 0, width / 2 * 3, height,
                new int[]{0x00ffffff, 0x88ffffff}, new float[]{0, 0.5f}, Shader.TileMode.MIRROR));

        mSuccessPath = new Path();
        mSuccessPath.moveTo(0, 0);
        mSuccessPath.rLineTo(width, 0);
        mSuccessPath.rLineTo(width / 2, height);
        mSuccessPath.rLineTo(-width, 0);
        mSuccessPath.close();
    }

    @Override
    protected ValueAnimator createSuccessAnimator() {
        ValueAnimator animator = ValueAnimator.ofInt(mView.getWidth(), 0);
        animator.setDuration(500);
        animator.setInterpolator(new FastOutLinearInInterpolator());
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                mSuccessAnimOffset = (int) animation.getAnimatedValue() - mView.getWidth() / 2;
                mView.invalidate();
            }
        });
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                isShowSuccessAnim = false;
            }

            @Override
            public void onAnimationStart(Animator animation) {
                isShowSuccessAnim = true;
            }
        });

        return animator;
    }

    @Override
    protected ValueAnimator createFailureAnimator() {
        ValueAnimator animator = ValueAnimator.ofInt();
        animator.setDuration(300);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int value = (int) animation.getAnimatedValue();
                mView.updateOffset(value);
            }
        });
        return animator;
    }

    @Override
    protected void drawSuccess(Canvas canvas) {
        if (isShowSuccessAnim) {
            canvas.translate(mSuccessAnimOffset, 0);
            canvas.drawPath(mSuccessPath, mSuccessPaint);
        }
    }

    @Override
    protected void drawFailure(Canvas canvas) {
        // todo ...
    }
}
